package main

import (
	"encoding/json"

	"jlpay-sdk-go/pkg/jlpay/demo"
	"jlpay-sdk-go/pkg/jlpay/models/trans/b2c"
	"jlpay-sdk-go/pkg/jlpay/models/trans/dto"
	serviceb2c "jlpay-sdk-go/pkg/jlpay/services/trans/b2c"
	"jlpay-sdk-go/pkg/jlpay/utils"
)

// 构建微支付请求
func buildMicroPayRequest() *b2c.MicroPayRequest {
	request := b2c.NewMicroPayRequest()

	// 设置请求参数
	request.SetMchID("849584358120018")
	request.SetTermNo("60131234")
	request.SetPayType("wxpay")
	request.SetOutTradeNo("ld201911023465529")
	request.SetPaymentValidTime("10")
	request.SetBody("充值中心-会员充值")
	request.SetAttach("商城—-易拉宝10个打印纸20箱")
	request.SetTotalFee("1")
	request.SetAuthCode("131208648101095993")
	request.SetLimitPay("no_credit")
	request.SetIsHirePurchase("1")
	request.SetHirePurchaseNum("3")
	request.SetHirePurchaseSellerPercent("0")
	request.SetRemark("该订单物流请走顺丰快递")
	request.SetOpUserID("001")
	request.SetOpShopID("GD_SZ_001")
	request.SetDeviceInfo("N50007A8890")
	request.SetMchCreateIP("123.12.12.123")
	request.SetLongitude("116.078631")
	request.SetLatitude("39.71274")
	request.SetSubAppid("wxid_129752pfasf1671")
	request.SetGoodsTag("sz001")

	// 设置商品详情
	goodsData := dto.NewGoodsDataDto()

	request.SetGoodsData(goodsData)

	// 设置终端信息
	terminalInfo := dto.NewTerminalInfoDto()

	request.SetTerminalInfo(terminalInfo)

	return request
}

// 微支付示例
func microPayDemo(microPayService *serviceb2c.MicroPayService) {
	// 创建微支付请求
	request := buildMicroPayRequest()

	// 序列化请求数据并打印
	requestMap, _ := request.ToMap()
	requestJSON, _ := json.Marshal(requestMap)
	utils.Debugf("微支付请求数据: %s", string(requestJSON))

	// 发送请求
	response, err := microPayService.MicroPay(request)
	if err != nil {
		utils.Errorf("微支付请求失败: %v", err)
		return
	}

	// 处理响应 - 使用BaseResponse接口的HandleResponse方法
	response.HandleResponse("微支付")
}

func main() {
	// 设置日志级别为info，显示详细的调试信息
	utils.SetLogLevel("info")

	utils.Info("=============================")
	utils.Info("===== 微支付示例 开始 =====")
	utils.Info("=============================")

	// 创建配置对象和微支付服务
	config := demo.CreateDemoConfig()
	microPayService := serviceb2c.NewMicroPayService(config)

	microPayDemo(microPayService)

	utils.Info("=============================")
	utils.Info("===== 微支付示例执行完毕 =====")
	utils.Info("=============================")
}
